from time import sleep
from selenium.webdriver.support.select import Select  # 正确：导入Selenium的Select

from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.wait import WebDriverWait

# 1.打开浏览器
driver = webdriver.Chrome(executable_path='../01/chromedriver.exe')
driver.maximize_window()
# 2.输入访问地址（URL）
driver.get("http://121.43.169.97:8848/pageA.html")


# --------------# 下标/索引--------------------
'''
el = driver.find_element(By.CSS_SELECTOR, "#selectA")  # 建议使用By类定位（更规范）
select = Select(el)
sleep(2)
select.select_by_index(2)  # 选择'广州'

sleep(2)
select.select_by_index(1)  # 选择'上海'

sleep(2)
select.select_by_index(0)  # 选择'北京'
'''

# --------------# value值实现--------------------
'''
el = driver.find_element(By.CSS_SELECTOR, "#selectA")
select = Select(el)
sleep(2)
select.select_by_value("gz")  # 选择'广州'

sleep(2)
select.select_by_value("sh")  # 选择'上海'

sleep(2)
select.select_by_value("bj")   # 选择'北京'
'''

# --------------# 文本值实现--------------------

el = driver.find_element(By.CSS_SELECTOR, "#selectA")
select = Select(el)
sleep(2)
select.select_by_visible_text("广州")  # 选择'广州'

sleep(2)
select.select_by_visible_text("上海")  # 选择'上海'

sleep(2)
select.select_by_visible_text("北京")  # 选择'北京'

# 最后别忘了关闭浏览器
driver.quit()